package com.daykw.xiaohongshu;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.daykw.xiaohongshu.task.ArticleJob;
import com.daykw.xiaohongshu.task.ClearIpPoolTask;
import com.daykw.xiaohongshu.task.CommentJob;
import com.daykw.xiaohongshu.task.IpPoolTask;
import com.daykw.xiaohongshu.task.RestArticleJob;
import com.daykw.xiaohongshu.task.RestCommentJob;
import com.daykw.xiaohongshu.task.SessionJob;
import com.daykw.xiaohongshu.task.thread.ArticleThread;
import com.daykw.xiaohongshu.task.thread.CommentThread;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.SessionUtils;
import com.daykw.xiaohongshu.util.SessionUtils.Session;
import com.daykw.xiaohongshu.util.SpringContextHolder;
import com.daykw.xiaohongshu.util.ZKC;

@SpringBootApplication
@ComponentScan(basePackages = "com.daykw")
@EnableAspectJAutoProxy(exposeProxy = true)
public class StartApp {
	private static Logger logger = Logger.getLogger(StartApp.class);

	public static void main(String[] args) throws Exception {

		ApplicationContext application = SpringApplication.run(StartApp.class, args);

		logger.info("##########spring boot 启动成功##########");
		SpringContextHolder.setApplicationContext(application);

		for (int i = 0; i < Const.ARTICLE_THREAD; i++) {
			ArticleThread xhsthread = new ArticleThread();
			xhsthread.start();
		}

		for (int i = 0; i < Const.COMMENT_THREAD; i++) {
			CommentThread cthread = new CommentThread();
			cthread.start();
		}
		logger.info("********翻页线程:" + Const.PAGE_THREAD);
		logger.info("********文章线程:" + Const.ARTICLE_THREAD);
		logger.info("********评论线程:" + Const.COMMENT_THREAD);

		int max = 18;
		int min = 10;
		Random random = new Random();

		int s1 = random.nextInt(max) % (max - min + 1) + min;

		ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
		// 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
		scheduled.scheduleAtFixedRate(new CommentJob(), 5, s1, TimeUnit.SECONDS);
		int s2 = random.nextInt(max) % (max - min + 1) + min;
		scheduled.scheduleAtFixedRate(new ArticleJob(), 5, s2, TimeUnit.SECONDS);
		int s3 = random.nextInt(max) % (max - min + 1) + min;
		scheduled.scheduleAtFixedRate(new IpPoolTask(), 5, s3, TimeUnit.SECONDS);
		int s4 = random.nextInt(max) % (max - min + 1) + min;
		scheduled.scheduleAtFixedRate(new ClearIpPoolTask(), 1, s4, TimeUnit.SECONDS);

		scheduled.scheduleAtFixedRate(new RestArticleJob(), 1, s4, TimeUnit.SECONDS);

		scheduled.scheduleAtFixedRate(new RestCommentJob(), 1, s4, TimeUnit.SECONDS);
		
		scheduled.scheduleAtFixedRate(new SessionJob(), 1, s4, TimeUnit.SECONDS);
		Session.session.getQueue();

		String ip = Const.getLocalHostIp();
		ZKC.getZKClient().doStart(ip);
		logger.info("全部启动完毕");
	}
}
